<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="菜单类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
              <j-dict-select-tag type="list" v-model="model.type" dictCode="sys_category,name,id, pid='1518882235254861826'" placeholder="请选择菜单类型" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="菜单名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.name" placeholder="请输入菜单名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="img">
              <imag-cropper v-model="model.img" :width="100" :height="100" :isCircle="true"></imag-cropper>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="跳转类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="linkType">
              <!--<j-dict-select-tag type="list" v-model="model.linkType" dictCode="sys_category,name,id, pid='1518495927034499073'" placeholder="请选择跳转类型" />-->
              <a-radio-group name="radioGroup" style="width:100%;"  v-model="model.linkType" @change="" button-style="solid">
                <a-radio-button :value="'1'">
                  一级跳转
                </a-radio-button>
                <a-radio-button :value="'2'">
                  二级跳转
                </a-radio-button>
                <a-radio-button :value="'3'">
                  三级跳转
                </a-radio-button>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="一级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="firstLevel">
              <j-dict-select-tag type="list" v-model="model.firstLevel" dictCode="sys_category,name,id, pid='1518507706603048961'"  placeholder="请选择一级" @change="firstLevelChange"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item v-if="(model.linkType=='2' || model.linkType=='3') && secondDict" label="二级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="secondLevel" >
              <j-dict-select-tag type="list" v-model="model.secondLevel" :dictCode="secondDict" placeholder="请选择二级" @change="secondeLevelChange"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item v-if="model.linkType=='3' && thirdDict" label="三级" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="thirdLevel">
              <j-search-select-tag v-model="model.thirdLevel" :dict="thirdDict"  />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="跳转路径" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="linkUrl">
              <a-textarea v-model="model.linkUrl" rows="4" placeholder="请输入跳转路径" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import ImagCropper from '../../../jeecg/ImagCropper'
  import store from '@/store'

  export default {
    name: 'TempleAppearanceMenuForm',
    components: {
      ImagCropper
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    watch:{
      model:{
        deep:true,
        handler(newVal, oldVal){
          let obj = newVal;
          if(obj.linkType=='1'){
            var firstLevel = obj.firstLevel
            if(firstLevel==this.GLOBAL_DATA.MODULE_NEWS){
              this.model.linkUrl = "/pages/news/category"
            }else if(firstLevel == this.GLOBAL_DATA.MODULE_ACTIVITY){
              this.model.linkUrl = "/pages/activity/category"
            }else if(firstLevel == this.GLOBAL_DATA.MODULE_DONATION){
              this.model.linkUrl = "/pages/donation/category"
            }
          }else if(obj.linkType=='2'){
            var firstLevel = obj.firstLevel

            if(firstLevel==this.GLOBAL_DATA.MODULE_NEWS){
              this.secondDict = "temple_category, name, id, sys_org_Code='"+this.userInfo.orgCode+"'"
            }else if(firstLevel == this.GLOBAL_DATA.MODULE_ACTIVITY){
              this.secondDict = "temple_activity_category, name, id, sys_org_Code='"+this.userInfo.orgCode+"'"
            }else if(firstLevel == this.GLOBAL_DATA.MODULE_DONATION){
              this.secondDict = "temple_donation_category, name, id, sys_org_Code='"+this.userInfo.orgCode+"'"
            }
            var secondLevel = obj.secondLevel
            if(secondLevel){
              if(firstLevel==this.GLOBAL_DATA.MODULE_NEWS){
                this.model.linkUrl = "/pages/news/list?categroyId="+secondLevel+"&name="+this.model.name //categroyId后台创建的时候单词拼写错了，讲错就错的用
              }else if(firstLevel == this.GLOBAL_DATA.MODULE_ACTIVITY){
                this.model.linkUrl = "/pages/activity/list?categoryId="+secondLevel+"&name="+this.model.name
              }else if(firstLevel == this.GLOBAL_DATA.MODULE_DONATION){
                this.model.linkUrl = "/pages/donation/list?categoryId="+secondLevel+"&name="+this.model.name
              }
            }
          }else if(obj.linkType=='3'){
            var firstLevel = obj.firstLevel
            var secondLevel = obj.secondLevel
            var thirdLevel = obj.thirdLevel

            if(firstLevel){
              if(firstLevel==this.GLOBAL_DATA.MODULE_NEWS){
                this.secondDict = "temple_category, name, id, sys_org_Code='"+this.userInfo.orgCode+"'"
              }else if(firstLevel == this.GLOBAL_DATA.MODULE_ACTIVITY){
                this.secondDict = "temple_activity_category, name, id, sys_org_Code='"+this.userInfo.orgCode+"'"
              }else if(firstLevel == this.GLOBAL_DATA.MODULE_DONATION){
                this.secondDict = "temple_donation_category, name, id, sys_org_Code='"+this.userInfo.orgCode+"'"
              }
            }

            if(firstLevel && secondLevel){

              if(firstLevel==this.GLOBAL_DATA.MODULE_NEWS){
                this.thirdDict = "temple_news, title, id, sys_org_Code='"+this.userInfo.orgCode+"' and  categroy_id='"+secondLevel+"'"
              }else if(firstLevel == this.GLOBAL_DATA.MODULE_ACTIVITY){
                this.thirdDict = "temple_activity, title, id, sys_org_Code='"+this.userInfo.orgCode+"' and  category_id='"+secondLevel+"'"
              }else if(firstLevel == this.GLOBAL_DATA.MODULE_DONATION){
                this.thirdDict = "temple_donation, name, id, sys_org_Code='"+this.userInfo.orgCode+"' and  category_id='"+secondLevel+"'"
              }

            }

            if(firstLevel && secondLevel && thirdLevel){
              if(firstLevel==this.GLOBAL_DATA.MODULE_NEWS){
                this.model.linkUrl = "/pages/news/detail?id="+thirdLevel+"&title="+this.model.name
              }else if(firstLevel == this.GLOBAL_DATA.MODULE_ACTIVITY){
                this.model.linkUrl = "/pages/activity/detail?id="+thirdLevel+"&title="+this.model.name
              }else if(firstLevel == this.GLOBAL_DATA.MODULE_DONATION){
                this.model.linkUrl = "/pages/donation/detail?id="+thirdLevel+"&title="+this.model.name
              }
            }

          }
        }

      },


    },
    data () {
      return {
        secondDict:null,
        thirdDict: null,
       userInfo:store.getters.userInfo,
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           type: [
              { required: true, message: '请输入菜单类型!'},
           ],
           name: [
              { required: true, message: '请输入菜单名称!'},
           ],
           img: [
              { required: true, message: '请输入图片!'},
           ],
           linkType: [
              { required: true, message: '请输入跳转类型!'},
           ],
           linkUrl: [
              { required: true, message: '请输入跳转路径!'},
           ],
        },
        url: {
          add: "/temple.appearance/templeAppearanceMenu/add",
          edit: "/temple.appearance/templeAppearanceMenu/edit",
          queryById: "/temple.appearance/templeAppearanceMenu/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      firstLevelChange(){
        if(this.model.linkType=='2' || this.model.linkType=='3' ){
          this.model.secondLevel=''
          this.model.thirdLevel=''
          this.model.linkUrl=''
        }
      },
      secondeLevelChange(){
        if(this.model.linkType=='3' ){
          this.model.thirdLevel=''
          this.model.linkUrl=''
        }
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>